package logic

import (
	"context"
	"fmt"
	"produce/model/mysql"
	"produce/model/redis"
	"produce/producerpc/internal/svc"
	"produce/producerpc/producerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *producerpc.UpdateRequest) (*producerpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	get := redis.Client.Get("sendsms" + in.Tel + in.Source)

	if get.Val() != in.Sendsms {
		return nil, fmt.Errorf("短信未验证")
	}

	var update mysql.User
	mysql.DB.Debug().Model(&update).Where("tel = ?", in.Tel).Find(&update)

	update.Password = in.Password
	mysql.DB.Debug().Model(&update).Updates(&update)

	return &producerpc.UpdateResponse{Id: int64(update.ID)}, nil
}
